Skip to content

F-1899 : fix potential heap buffer over-read#220

Open
miyazakh wants to merge 4 commits intowolfSSL:mainfrom
miyazakh:f-1899_heapbuffer_over-read
Open

F-1899 : fix potential heap buffer over-read#220
miyazakh wants to merge 4 commits intowolfSSL:mainfrom
miyazakh:f-1899_heapbuffer_over-read

Conversation

@miyazakh
Copy link
Copy Markdown
Contributor

@miyazakh miyazakh commented Apr 8, 2026

Fix potential heap buffer over-read
Refactor wolfCLU_sign_data_dilithium
Add test coverage

Depend on : #219

Copilot AI review requested due to automatic review settings April 8, 2026 07:24
@miyazakh miyazakh self-assigned this Apr 8, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses robustness and safety in signing/verification workflows, primarily targeting Dilithium signing error handling to prevent potential heap misuse, and extends regression coverage via shell tests.

Changes:

  • Refactors wolfCLU_sign_data_dilithium to improve cleanup/error-path handling and avoid unsafe buffer usage patterns.
  • Updates ECC sign/verify to hash input data before calling ECDSA primitives.
  • Adds Dilithium negative-path regression tests to ensure failures don’t create output files.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
tests/genkey_sign_ver/genkey-sign-ver-test.sh Adds Dilithium failure-mode tests (invalid output path, wrong key type, corrupted key) and cleans up new artifacts.
src/x509/clu_x509_sign.c Adjusts wolfSSL_BIO_get_fp calls (casts) while loading key material for Chimera cert signing.
src/sign-verify/clu_verify.c Changes ECC verification to hash input data prior to wc_ecc_verify_hash.
src/sign-verify/clu_sign.c Changes ECC signing to hash input data prior to wc_ecc_sign_hash; refactors Dilithium signing for safer handling.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/sign-verify/clu_sign.c Outdated
Comment thread src/sign-verify/clu_sign.c Outdated
Comment thread src/sign-verify/clu_verify.c
Comment thread src/sign-verify/clu_sign.c
@miyazakh miyazakh force-pushed the f-1899_heapbuffer_over-read branch from dc2f472 to 4328349 Compare April 8, 2026 23:25
@miyazakh miyazakh marked this pull request as ready for review April 8, 2026 23:41
Copilot AI review requested due to automatic review settings April 8, 2026 23:41
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/sign-verify/clu_sign.c
Comment thread src/sign-verify/clu_sign.c
Comment thread src/sign-verify/clu_sign.c
Comment thread src/sign-verify/clu_sign.c
@miyazakh miyazakh assigned wolfSSL-Bot and unassigned miyazakh Apr 9, 2026
Copy link
Copy Markdown
Contributor

@dgarske dgarske left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐺 Skoll Code Review

Overall recommendation: REQUEST_CHANGES
Findings: 3 total — 2 posted, 1 skipped

Posted findings

  • [High] wc_dilithium_init failure not captured in ret — execution continues on errorsrc/sign-verify/clu_sign.c:648-650
  • [Medium] Error log for allocation failure prints stale ret value (always 0)src/sign-verify/clu_sign.c:724-725
Skipped findings
  • [Info] Extra blank line where XMEMSET was removed

Review generated by Skoll via openclaw

Comment thread src/sign-verify/clu_sign.c Outdated
Comment thread src/sign-verify/clu_sign.c
@dgarske dgarske assigned miyazakh and unassigned wolfSSL-Bot Apr 10, 2026
@miyazakh miyazakh force-pushed the f-1899_heapbuffer_over-read branch from 620f391 to 7d03ee9 Compare April 17, 2026 04:14
Copilot AI review requested due to automatic review settings April 17, 2026 04:33
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

keyFile = XFOPEN(keyPath, "rb");
if (keyFile == NULL) {
wolfCLU_LogError("Faild to open Private key FILE.");
wolfCLU_LogError("Failed to open Private key FILE.");
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error path when opening the Dilithium public key logs "Failed to open Private key FILE." even though this function is reading a public key (see the preceding comment "open and read public key"). Please correct the message to reference the public key to avoid confusing diagnostics.

Suggested change
wolfCLU_LogError("Failed to open Private key FILE.");
wolfCLU_LogError("Failed to open Public key FILE.");

Copilot uses AI. Check for mistakes.
Comment on lines +791 to +792
XFWRITE(outBuf, 1, outBufSz, outFile);
XFCLOSE(outFile);
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signature write success isn’t validated: if XFWRITE() fails (disk full, permission issues, etc.), ret remains 0 and the function will return success. Check the XFWRITE() result and convert failures into an error code (and ideally avoid leaving a partially-written output file).

Suggested change
XFWRITE(outBuf, 1, outBufSz, outFile);
XFCLOSE(outFile);
size_t written = XFWRITE(outBuf, 1, outBufSz, outFile);
if (written != outBufSz) {
wolfCLU_LogError("Failed to write signature to output file %s",
out);
ret = IO_FAILED_E;
}
if (XFCLOSE(outFile) != 0 && ret == 0) {
wolfCLU_LogError("Failed to close output file %s", out);
ret = IO_FAILED_E;
}
if (ret != 0) {
XREMOVE(out);
}

Copilot uses AI. Check for mistakes.
Comment on lines +359 to +360
with open(corrupt_key, "w") as f:
f.write("INVALID KEY DATA")
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test writes a file intended to be parsed as DER (-inform der), but opens it in text mode ("w"). On Windows this can introduce newline translation and make the test less predictable. Open the corrupted key file in binary mode ("wb") for consistency with other tests (e.g., RSA invalid key test).

Suggested change
with open(corrupt_key, "w") as f:
f.write("INVALID KEY DATA")
with open(corrupt_key, "wb") as f:
f.write(b"INVALID KEY DATA")

Copilot uses AI. Check for mistakes.
Comment on lines +735 to +739
if (XFSEEK(privKeyFile, 0, SEEK_SET) != 0 ||
(int)XFREAD(privBuf, 1, privFileSz, privKeyFile) != privFileSz) {
wolfCLU_LogError("Incorrect private key file size: %d", privFileSz);
ret = WOLFCLU_FATAL_ERROR;
}
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This failure condition covers both seek/read errors (not just size mismatches), but the log message always reports an "Incorrect private key file size". Consider logging a more accurate read/seek failure message (or splitting the checks) to make debugging I/O errors easier.

Suggested change
if (XFSEEK(privKeyFile, 0, SEEK_SET) != 0 ||
(int)XFREAD(privBuf, 1, privFileSz, privKeyFile) != privFileSz) {
wolfCLU_LogError("Incorrect private key file size: %d", privFileSz);
ret = WOLFCLU_FATAL_ERROR;
}
if (XFSEEK(privKeyFile, 0, SEEK_SET) != 0) {
wolfCLU_LogError("Failed to seek to beginning of private key file.");
ret = WOLFCLU_FATAL_ERROR;
}
else if ((int)XFREAD(privBuf, 1, privFileSz, privKeyFile) != privFileSz) {
wolfCLU_LogError("Failed to read private key file: expected %d bytes.",
privFileSz);
ret = WOLFCLU_FATAL_ERROR;
}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants